﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.IO;
using ZedGraph;

namespace LED_Tester
{
    public partial class frmR3 : Form
    {
        public frmR3()
        {
            InitializeComponent();
        }

        #region 变量声明

        private string[][] dataCIE;
        private PointPairList pplCIE1931 = new PointPairList();
        private PointPairList pplCIE1960 = new PointPairList();
        private PointPairList pplICE1976 = new PointPairList();
        private LineItem litCIE1931;
        private LineItem litCIE1960;
        private LineItem litCIE1976;

        private string[][] dataSDCM;
        private int[] ANSITc;// 用于保存ANSI色温数据
        private int[] IECTc;// 用于保存IEC色温数据
        private int ANSIIndex;// 用于保存ANSI数量
        private int IECIndex;// 用于保存IEC数量
        private double[][] ANSIParam;// 用于保存ANSI计算SDCM参数
        private double[][] IECParam;//用于保存IEC计算SDCM参数
        private PointPairList pplSDCM = new PointPairList();// 用于打点
        private LineItem litSDCM;

        private int i_IndexSDCM = 1;
        private int rankLo = 1;
        private int rankHi = 10;

        public int I_indexSDCM
        {
            get { return i_IndexSDCM; }
            set { i_IndexSDCM = value; }
        }
        public int RankLo
        {
            get { return rankLo; }
            set { rankLo = value; }
        }
        public int RankHi
        {
            get { return rankHi; }
            set { rankHi = value; }
        }

        private double[] smallxData;
        private double[] smallyData;

        public double[] SmallxData
        {
            get { return smallxData; }
            set { smallxData = value; }
        }
        public double[] SmallyData
        {
            get { return smallyData; }
            set { smallyData = value; }
        }


        #endregion

        #region 功能函数

        private void initailize()
        {
            drawCIE(zed1931, Application.StartupPath + "\\data\\CIE1931.txt");
            drawCIE(zed1960, Application.StartupPath + "\\data\\CIE1960.txt");
            drawCIE(zed1976, Application.StartupPath + "\\data\\CIE1976.txt");

            drawSDCM(zedSDCM, Application.StartupPath + "\\data\\SDCM.txt", i_IndexSDCM, rankLo, rankHi);
        }

        private void readCIE(string fileName)
        {
            if (!File.Exists(fileName))
            {
                return;
            }

            FileStream fileStream = new FileStream(fileName, FileMode.Open, FileAccess.Read);

            if (fileStream == null)
            {
                return;
            }

            StreamReader streamReader = new StreamReader(fileStream);

            int index = 0;
            dataCIE = new string[80][];

            try
            {
                while (!streamReader.EndOfStream)
                {
                    string str = "";
                    str = streamReader.ReadLine();
                    string[] temp = new string[2];
                    temp = str.Split("	".ToCharArray());
                    dataCIE[index] = temp;
                    index++;
                }
            }
            catch { }
            finally
            {
                streamReader.Close();
                fileStream.Close();
            }
        }

        private void drawCIE(ZedGraphControl zed, string fileName)
        {
            zed.GraphPane.CurveList.Clear();

            zed.GraphPane.Chart.Fill = new ZedGraph.Fill(Color.White);
            zed.IsShowContextMenu = true; // 设置右键功能选项
            zed.IsShowPointValues = true;
            zed.PointValueFormat = "#0.0000";
            zed.GraphPane.Title.Text = "";

            zed.GraphPane.XAxis.Title.Text = "x";
            zed.GraphPane.XAxis.MajorGrid.IsVisible = true;
            zed.GraphPane.XAxis.Scale.Max = 0.8f;
            zed.GraphPane.XAxis.MajorGrid.PenWidth = 1f;

            zed.GraphPane.YAxis.Title.Text = "y";
            zed.GraphPane.YAxis.MajorGrid.IsVisible = true;
            zed.GraphPane.YAxis.Scale.Max = 0.9f;
            zed.GraphPane.YAxis.MajorGrid.PenWidth = 1f;

            readCIE(fileName);

            PointPairList pplList = new PointPairList();
            for (int i = 0; i < dataCIE.Length; i++)
            {
                double x = double.Parse(dataCIE[i][0].ToString());
                double y = double.Parse(dataCIE[i][1].ToString());
                pplList.Add(x, y);
            }
            pplList.Add(double.Parse(dataCIE[0][0].ToString()), double.Parse(dataCIE[0][1].ToString()));
            pplList.Add(double.Parse(dataCIE[79][0].ToString()), double.Parse(dataCIE[79][1].ToString()));

            LineItem litLine = zed.GraphPane.AddCurve("", pplList, Color.Blue, SymbolType.None);

            zed.AxisChange();
            zed.Refresh();
        }

        private void pointCIE(double[] x, double[] y)
        {
            pplCIE1931.Clear();
            pplCIE1960.Clear();
            pplICE1976.Clear();

            for (int i = 0; i < x.Length; i++)
            {
                pplCIE1931.Add(x[i], y[i]);

                double u = 4 * x[i] / (-2 * x[i] + 12 * y[i] + 3);
                double v = 6 * y[i] / (-2 * x[i] + 12 * y[i] + 3);

                pplCIE1960.Add(u, v);

                double vDush = v / 3 * 2;

                pplICE1976.Add(u, vDush);
            }

            litCIE1931 = zed1931.GraphPane.AddCurve("", pplCIE1931, Color.Red, SymbolType.XCross);
            litCIE1960 = zed1960.GraphPane.AddCurve("", pplCIE1960, Color.Red, SymbolType.XCross);
            litCIE1976 = zed1976.GraphPane.AddCurve("", pplICE1976, Color.Red, SymbolType.XCross);

            litCIE1931.Line.IsVisible = false;
            litCIE1960.Line.IsVisible = false;
            litCIE1976.Line.IsVisible = false;

            zed1931.Refresh();
            zed1960.Refresh();
            zed1976.Refresh();
        }

        private void readSDCM(string fileName)
        {
            if (!File.Exists(fileName))
            {
                return;
            }

            FileStream fileStream = new FileStream(fileName, FileMode.Open, FileAccess.Read);

            if (fileStream == null)
            {
                return;
            }

            StreamReader streamReader = new StreamReader(fileStream);

            int i = 0;
            dataSDCM = new string[12][];
            try
            {
                while (!streamReader.EndOfStream)
                {
                    string str = "";
                    str = streamReader.ReadLine();
                    string[] data = new string[17];
                    data = str.Split(" ".ToCharArray());
                    dataSDCM[i] = data;//每一行所有数据
                    i++;
                }
            }
            catch { }
            finally
            {
                streamReader.Close();
                fileStream.Close();
            }

            ANSITc = new int[6];//色温
            IECTc = new int[6];//色温
            ANSIParam = new double[6][];//色温对应的系数
            IECParam = new double[6][];//色温对应的系数
            ANSIIndex = 0;//索引
            IECIndex = 0;//索引
            double[] temp;
            for (int j = 0; j < dataSDCM.Length; j++)
            {
                if (dataSDCM[j][0].Substring(0, 4) == "ANSI")
                {
                    temp = new double[10];
                    ANSITc[ANSIIndex] = int.Parse(dataSDCM[j][0].Substring(5, 4));
                    temp[0] = double.Parse(dataSDCM[j][9]);//x1
                    temp[1] = double.Parse(dataSDCM[j][10]);//y1
                    temp[2] = double.Parse(dataSDCM[j][3]);//g11
                    temp[3] = double.Parse(dataSDCM[j][4]);//g12
                    temp[4] = double.Parse(dataSDCM[j][5]);//g13
                    temp[5] = double.Parse(dataSDCM[j][6]);//angle
                    temp[6] = double.Parse(dataSDCM[j][1]);//x
                    temp[7] = double.Parse(dataSDCM[j][2]);//y
                    temp[8] = double.Parse(dataSDCM[j][7]);//a
                    temp[9] = double.Parse(dataSDCM[j][8]);//b
                    ANSIParam[ANSIIndex] = temp;
                    ANSIIndex++;
                }
                else if (dataSDCM[j][0].Substring(0, 3) == "IEC")
                {
                    temp = new double[10];
                    IECTc[IECIndex] = int.Parse(dataSDCM[j][0].Substring(5, 4));
                    temp[0] = double.Parse(dataSDCM[j][9]);//x1
                    temp[1] = double.Parse(dataSDCM[j][10]);//y1
                    temp[2] = double.Parse(dataSDCM[j][3]);//g11
                    temp[3] = double.Parse(dataSDCM[j][4]);//g12
                    temp[4] = double.Parse(dataSDCM[j][5]);//g13
                    temp[5] = double.Parse(dataSDCM[j][6]);//angle
                    temp[6] = double.Parse(dataSDCM[j][1]);//x
                    temp[7] = double.Parse(dataSDCM[j][2]);//y
                    temp[8] = double.Parse(dataSDCM[j][7]);//a
                    temp[9] = double.Parse(dataSDCM[j][8]);//b
                    IECParam[IECIndex] = temp;
                    IECIndex++;
                }
            }
        }

        private void drawSDCM(ZedGraphControl zed, string fileName, int i_indexSDCM, int rankLo, int rankHi)
        {
            #region 初始化

            zed.GraphPane.Chart.Fill = new ZedGraph.Fill(Color.White, Color.LightGoldenrodYellow, 45.0f);// 填充背景色
            zed.IsShowContextMenu = true;
            zed.IsShowPointValues = true;// 点设置
            zed.PointDateFormat = "#0.0000";
            zed.GraphPane.Title.Text = "";// 标题设置
            // X轴设置
            zed.GraphPane.XAxis.Title.Text = "";
            zed.GraphPane.XAxis.Scale.Min = 0;
            zed.GraphPane.XAxis.Scale.Max = 0.9;
            zed.GraphPane.XAxis.MajorGrid.IsVisible = false;
            // Y轴设置
            zed.GraphPane.YAxis.Title.Text = "";
            zed.GraphPane.YAxis.Scale.Min = 0;
            zed.GraphPane.YAxis.Scale.Max = 0.8;
            zed.GraphPane.YAxis.MajorGrid.IsVisible = false;
            zed.GraphPane.YAxis.MajorGrid.IsZeroLine = false;// Y=0不显示

            zed.GraphPane.AxisChange();

            readSDCM(fileName);// 读取SDCM标准数据

            #endregion

            #region 选择标准

            int i = -1;
            int j = -1;

            if (i_IndexSDCM == 0)
            {
                i = 0;
            }
            else if (i_IndexSDCM == 1)
            {
                i = 1;
            }
            else if (i_IndexSDCM == 2)
            {
                i = 2;
            }
            else if (i_IndexSDCM == 3)
            {
                i = 3;
            }
            else if (i_IndexSDCM == 4)
            {
                i = 4;
            }
            else if (i_IndexSDCM == 5)
            {
                i = 5;
            }
            else if (i_IndexSDCM == 6)
            {
                j = 0;
            }
            else if (i_IndexSDCM == 7)
            {
                j = 1;
            }
            else if (i_IndexSDCM == 8)
            {
                j = 2;
            }
            else if (i_IndexSDCM == 9)
            {
                j = 3;
            }
            else if (i_IndexSDCM == 10)
            {
                j = 4;
            }
            else if (i_IndexSDCM == 11)
            {
                j = 5;
            }

            double angle = 0, x = 0, y = 0, a = 0, b = 0;

            if (i != -1)
            {
                angle = ANSIParam[i][5];
                x = ANSIParam[i][6];
                y = ANSIParam[i][7];
                a = ANSIParam[i][8];
                b = ANSIParam[i][9];
            }
            if (j != -1)
            {
                angle = ANSIParam[j][5];
                x = ANSIParam[j][6];
                y = ANSIParam[j][7];
                a = ANSIParam[j][8];
                b = ANSIParam[j][9];
            }

            #endregion

            #region 画图

            if (zed.GraphPane.CurveList.Count > 0)
            {
                zed.GraphPane.CurveList[0].Clear();
                zed.GraphPane.CurveList[1].Clear();
                zed.GraphPane.CurveList[2].Clear();
            }
            double x1, y1, x2, y2, r;
            angle = angle / 180 * Math.PI;

            PointPairList McAdamPoint1 = new ZedGraph.PointPairList();
            PointPairList McAdamPoint2 = new ZedGraph.PointPairList();
            PointPairList xyPoint = new ZedGraph.PointPairList();
            for (int k = 0; k < 165; k++)
            {
                r = -Math.PI + 0.04f * k;
                x1 = rankLo * a * Math.Sin(r) * Math.Cos(angle) - rankLo * b * Math.Sin(angle) * Math.Cos(r) + x;
                y1 = rankLo * a * Math.Sin(r) * Math.Sin(angle) + rankLo * b * Math.Cos(angle) * Math.Cos(r) + y;
                x2 = rankHi * a * Math.Sin(r) * Math.Cos(angle) - rankHi * b * Math.Sin(angle) * Math.Cos(r) + x;
                y2 = rankHi * a * Math.Sin(r) * Math.Sin(angle) + rankHi * b * Math.Cos(angle) * Math.Cos(r) + y;
                McAdamPoint1.Add(x1, y1);
                McAdamPoint2.Add(x2, y2);
            }
            LineItem McAdamLine1 = zed.GraphPane.AddCurve("", McAdamPoint1, Color.Green, SymbolType.None);
            LineItem McAdamLine2 = zed.GraphPane.AddCurve("", McAdamPoint2, Color.Green, SymbolType.None);

            zed.GraphPane.CurveList[0] = McAdamLine1;
            zed.GraphPane.CurveList[1] = McAdamLine2;

            xyPoint.Add(x, y);
            LineItem xyLine = zed.GraphPane.AddCurve("", xyPoint, Color.Brown, ZedGraph.SymbolType.Triangle);
            zed.GraphPane.CurveList[2] = xyLine;
            // 坐标变换
            zed.GraphPane.XAxis.Scale.Min = x - x * 0.1;
            zed.GraphPane.XAxis.Scale.Max = x + x * 0.1;
            zed.GraphPane.YAxis.Scale.Min = y - y * 0.1;
            zed.GraphPane.YAxis.Scale.Max = y + y * 0.1;

            pplSDCM.Clear();

            litSDCM = zed.GraphPane.AddCurve("", pplSDCM, Color.Red, SymbolType.XCross);
            litSDCM.Line.IsVisible = false;

            zed.Refresh();

            #endregion
        }

        private void pointSDCM(double[] x, double[] y)
        {
            pplSDCM.Clear();

            for (int i = 0; i < x.Length; i++)
            {
                pplSDCM.Add(x, y);
            }

            litSDCM = zedSDCM.GraphPane.AddCurve("", pplSDCM, Color.Red, SymbolType.XCross);
            litSDCM.Line.IsVisible = false;

            zedSDCM.Refresh();
        }

        public void SelectSDCM()
        {
            drawSDCM(zedSDCM, Application.StartupPath + "\\data\\SDCM.txt", i_IndexSDCM, rankLo, rankHi);

            if (smallxData != null && smallyData != null)
            {
                if (smallxData.Max() > 0 && smallyData.Max() > 0)
                {
                    pointSDCM(smallxData, smallyData);
                }
            }
        }

        public void Display()
        {
            pointCIE(smallxData, smallyData);

            pointSDCM(smallxData, smallyData);
        }

        public void Clear()
        {
            pplCIE1931.Clear();
            pplCIE1960.Clear();
            pplICE1976.Clear();
            pplSDCM.Clear();

            zed1931.Refresh();
            zed1960.Refresh();
            zed1976.Refresh();
            zedSDCM.Refresh();
        }

        public double CalcSDCM(double x, double y, int i_IndexSDCM)
        {
            double result = -1;

            int i = -1;
            int j = -1;

            if (i_IndexSDCM == 0)
            {
                i = 0;
            }
            else if (i_IndexSDCM == 1)
            {
                i = 1;
            }
            else if (i_IndexSDCM == 2)
            {
                i = 2;
            }
            else if (i_IndexSDCM == 3)
            {
                i = 3;
            }
            else if (i_IndexSDCM == 4)
            {
                i = 4;
            }
            else if (i_IndexSDCM == 5)
            {
                i = 5;
            }
            else if (i_IndexSDCM == 6)
            {
                j = 0;
            }
            else if (i_IndexSDCM == 7)
            {
                j = 1;
            }
            else if (i_IndexSDCM == 8)
            {
                j = 2;
            }
            else if (i_IndexSDCM == 9)
            {
                j = 3;
            }
            else if (i_IndexSDCM == 10)
            {
                j = 4;
            }
            else if (i_IndexSDCM == 11)
            {
                j = 5;
            }

            if (i != -1)
            {
                double x1 = ANSIParam[i][0];
                double y1 = ANSIParam[i][1];
                double g11 = ANSIParam[i][2];
                double g12 = ANSIParam[i][3];
                double g22 = ANSIParam[i][4];
                x -= x1;
                y -= y1;
                result = g11 * Math.Pow(x, 2) + 2 * g12 * x * y + g22 * Math.Pow(y, 2);
            }
            if (j != -1)
            {
                double x1 = IECParam[j][0];
                double y1 = IECParam[j][1];
                double g11 = IECParam[j][2];
                double g12 = IECParam[j][3];
                double g22 = IECParam[j][4];
                x -= x1;
                y -= y1;
                result = Math.Sqrt(g11 * Math.Pow(x, 2) + 2 * g12 * x * y + g22 * Math.Pow(y, 2));
            }
            return result;
        }

        #endregion

        #region 窗体事件

        private void frmR3_Load(object sender, EventArgs e)
        {
            initailize();
        }

        private void frmR3_FormClosing(object sender, FormClosingEventArgs e)
        {
            this.Hide();
            e.Cancel = true;
        }

        #endregion

        #region 其它事件

        private void tlmRestore_Click(object sender, EventArgs e)
        {

        }

        #endregion
    }
}
